<?php

namespace app\api\model;

use app\api\traits\Common;
use think\Model;

class User extends \app\admin\model\model\system\User
{

    // 根据id获取手机号
    public static function getPhoneByUser($id) {
        $where = [
            'id' => $id,
        ];
        return self::getValueCommon($where, 'phone');
    }

    // 注销账号
    public static function logoff($userId) {
        $where = [
            'id' => $userId,
        ];
        $data = [
            'is_logoff' => self::IS_LOGOFF_YES,
        ];
        return self::updateCommon($data, $where);
    }

    /**
     * @param $id
     * @return User|array|mixed|Model|null|self
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getInfo($id) {
        return self::getLineByPkCommon($id, '*');
    }

    /**
     * 根据手机号获取用户信息
     * @param $phone
     * @return Common|array|mixed|Model|null|User
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getByPhone($phone) {
        $where = [
            'phone' => $phone,
        ];
        return self::getLineCommon($where);
    }

    /**
     * 根据手机号注册,返回用户ID
     * @param $phone
     * @return mixed
     */
    public static function registerByPhone($phone) {
        $data = [
            'phone'    => $phone,
            'nickname' => $phone,
            'image' => '2021/headimg/1637668476021.png',
        ];
        return self::addCommon($data);
    }

    public static function changeNickName($userId, $nickName) {
        $where = [
            'id' => $userId,
        ];
        $data = [
            'nickname' => $nickName,
        ];
        return self::updateCommon($data, $where);
    }

    public static function changeImage($userId, $portrait) {
        $where = [
            'id' => $userId,
        ];
        $data = [
            'image' => $portrait,
        ];
        return self::updateCommon($data, $where);
    }

}